local skel = fk.CreateSkill{
  name = "re_heg__chenglue",
}
local H = require "packages/hegemony/util"
---@type TrigSkelSpec<AimFunc>
local chenglue = {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and H.compareKingdomWith(target, player) and #AimGroup:getAllTargets(data.tos) > 1 and data.firstTarget
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#re_heg__chenglue-ask:" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    if target:isAlive() then
      target:drawCards(1, self.name)
    end
    data.extra_data = data.extra_data or {}
    data.extra_data.re_heg__chenglueUser = data.extra_data.re_heg__chenglueUser or {}
    data.extra_data.re_heg__chenglueUser[player.id] = true
  end,
}
---@type TrigSkelSpec<UseCardFunc>
local chenglue_after = {
  mute = true,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return ((data.extra_data or {}).re_heg__chenglueUser or {})[player.id]
      and data.damageDealt and data.damageDealt[player.id]
  end,
  on_cost = function(self, event, target, player, data)
    local targets = table.map(table.filter(player.room.alive_players, function(p)
      return H.compareKingdomWith(p, player)
        and p:getMark("@!yinyangfish") == 0 and p:getMark("@!companion") == 0
        and p:getMark("@!wild") == 0 and p:getMark("@!vanguard") == 0
    end), Util.IdMapper)
    if #targets == 0 then return end
    local to = player.room:askForChoosePlayers(player, targets, 1, 1, "#re_heg__chenglue-give", self.name, true)
    if #to == 1 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    H.addHegMark(player.room, player.room:getPlayerById(self.cost_data), "yinyangfish")
  end,
}
skel:addEffect(fk.TargetSpecified, chenglue)
skel:addEffect(fk.CardUseFinished, chenglue_after)
return skel